/**
 *grid
 */
/*
var a = {
        priKey:"MODULE_DM",
        header:["模块代码","模块名称","模块地址"],
        column:["MODULE_DM","MODULE_NAME","MODULE_URL"],
        aligns:["center","left","left"],
        data:json.data
    };
    myGrid.grid(a);
 */
function myGrid() {
}
/**
 *创建grid
 */
myGrid.grid = function(json) {
    var strTb = "";
    strTb = "<table class='datagrid2'>";
    //显示表头
    strTb += "<tr>";
    strTb += "<th width='30'><a href='javascript:myGrid.checkAll()'>全选</a></th>";
    for (var n = 0; n < json.header.length; n++) {
        if (json.width)
            strTb += "<th width='" + json.width[n] + "'>";
        else
            strTb += "<th>";
        strTb += json.header[n];
        strTb += "</th>";
    }
    strTb += "</tr>";
    //显示数据
    for (var i = 0; i < json.data.data.length; i++) {
        var list = json.data.data[i];
        strTb += "<tr>";
        
        var isSelected = "";
        if(list.isSelected&&list.isSelected != "0"){
        	isSelected = "checked";
        }
        
        strTb += "<td align='center'><input type='checkbox' name='options' value='" + list[json.priKey] + "' " + isSelected + "></td>";
        var col = json.column;
        for (var n = 0; n < col.length; n++) {
            var colName = col[n];
            var value = list[colName];
            value = value==null?"":value;
            var align = json.aligns[n] ? json.aligns[n] : "";
            strTb += "<td align='" + align + "'>";
            strTb += value;
            strTb += "</td>";
        }
        strTb += "</tr>";
    }
    strTb += "</table>";
    if (json.data.pages) {
        var page = json.data.pages;
        strTb += "<div style='width:98%;text-align:right;'>";
        strTb += "<a href='javascript:" + json.pageFun + "(\"" + 0 + "\")'>首页</a> ";
        strTb += "<a href='javascript:" + json.pageFun + "(\"" + this.getPage(parseInt(page.CURPAGE) - 1, page.TOTALPAGE) + "\")'>上一页</a> ";
        strTb += "<a href='javascript:" + json.pageFun + "(\"" + this.getPage(parseInt(page.CURPAGE) + 1, page.TOTALPAGE) + "\")'>下一页</a> ";
        strTb += "<a href='javascript:" + json.pageFun + "(\"" + (parseInt(page.TOTALPAGE) - 1) + "\")'>尾页</a>";
        strTb += "  共" + page.TOTALCOUNT + "条记录";
        strTb += "  每页显示" + page.PAGESIZE + "条";
        strTb += "  页次" + (page.CURPAGE * 1 + 1) + "/" + page.TOTALPAGE;
        strTb += "</div>";
    }
    document.getElementById("editAreaDiv").innerHTML = strTb;
    setTableCss();
    //样式
};
/**
 *单选 取得选中的记录的id
 */
myGrid.getSelectedID = function() {
    var options = document.getElementsByName("options");
    var flg = false;
    var id = "";
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            if (flg) {
                alert("不能多选！");
                return false;
            }
            id = options[i].value;
            flg = true;
        }

    }
    if (flg) {
        return id;
    } else {
        alert("请选择操作对象！");
        return false;
    }
};
/**
 *取得选中的记录的id 允许多选， 以逗号分隔返回
 */
myGrid.getSelectedIDS = function() {
    var options = document.getElementsByName("options");
    var flg = false;
    var id = "";
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            var val = options[i].value;
            id = id.length > 0 ? (id + "," + val) : val;
            flg = true;
        }

    }
    if (flg) {
        return id;
    } else {
        alert("请选择操作对象！");
        return false;
    }
};
/**
 *grid 全选
 */
myGrid.checkAll = function() {
    var options = document.getElementsByName("options");
    var flag = false;
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            flag = true;
            break;
        }
    }
    if (flag) {
        for (var i = 0; i < options.length; i++) {
            options[i].checked = false;
        }
    } else {
        for (var i = 0; i < options.length; i++) {
            options[i].checked = true;
        }
    }
};
myGrid.getPage = function(curPage, totalPage) {
    if (curPage <= 0) {
        return 0;
    } else if (parseInt(curPage) >= parseInt(totalPage)) {
        return  parseInt(totalPage) - 1;
    } else {
        return curPage;
    }
};
function add_event(tr) {
    tr.onmouseover = function() {
        tr.className += ' hover';
    };
    tr.onmouseout = function() {
        tr.className = tr.className.replace(' hover', '');
    };
}
function stripe(table) {
    var trs = table.getElementsByTagName("tr");
    for (var i = 1; i < trs.length; i++) {
        var tr = trs[i];
        tr.className = i % 2 != 0 ? 'odd' : 'even';
        add_event(tr);
    }
}
function setTableCss() {
    var tables = document.getElementsByTagName('table');
    for (var i = 0; i < tables.length; i++) {
        var table = tables[i];
        if (table.className == 'datagrid1' || table.className == 'datagrid2'
                || table.className == 'datagrid3' || table.className == 'datagrid4') {
            stripe(tables[i]);
        }
    }
}